package com.example.list;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ViewSwitcher;

public class PoppupWindowActivity extends AppCompatActivity {

    private View mPopView;
    private PopupWindow mPopupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_poppup_window);
        getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
                initPopWindow();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
    }


    private void initPopWindow() {
        // TODO Auto-generated method stub
        // 将布局文件转换成View对象，popupview 内容视图
//        mPopView = getLayoutInflater().inflate(R.layout.layout_popup_calendar, null);
        mPopView = LayoutInflater.from(this).inflate(R.layout.layout_popup_setting, null);
        // 将转换的View放置到 新建一个popuwindow对象中
        mPopupWindow = new PopupWindow(mPopView,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        setBackgroundAlpha(0.5f);
        // 点击popuwindow外让其消失
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setTouchable(true);

        ViewSwitcher switcher_saveAccount = (ViewSwitcher) mPopView.findViewById(R.id.viewSwitcher_saveAccount);
        ViewSwitcher switcher_protectAccount = (ViewSwitcher) mPopView.findViewById(R.id.viewSwitcher_protectAccount);
        SeekBar mOnLineTimeSeekBar = (SeekBar) mPopView.findViewById(R.id.online_set_Seekbar);
        TextView mTimeText = (TextView) mPopView.findViewById(R.id.tv_time);
        ImageView img_close = mPopView.findViewById(R.id.img_close);
        img_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
            }
        });
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1);
            }
        });


//        switcher_protectAccount.showNext();
        switcher_saveAccount.setDisplayedChild(1);
        switcher_protectAccount.setDisplayedChild(0);
        switcher_saveAccount.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                ViewSwitcher vSwitcher = (ViewSwitcher) arg0;
                if (vSwitcher.getDisplayedChild() == 0) {
                    vSwitcher.showNext();
                } else {
                    vSwitcher.showPrevious();
                }
            }
        });
        switcher_protectAccount.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                ViewSwitcher vSwitcher = (ViewSwitcher) arg0;
                if (vSwitcher.getDisplayedChild() == 0) {
                    vSwitcher.showNext();
                } else {
                    vSwitcher.showPrevious();
                }
            }
        });
        mOnLineTimeSeekBar.setProgress(10);
        mOnLineTimeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//                int min = isToday ? mMinValue : mMinDaysValue;
//                int mShowValue = progress + min;
                mTimeText.setText(String.valueOf(progress) + "分");
            }
        });


        mPopupWindow.showAtLocation(mPopView, Gravity.BOTTOM, 0, 0);
    }

    public void Click(View view) {
        initPopWindow();//必须等到界面初始化后  才能弹出popupwindow，否则会崩溃报错
        //所以加上了延时10ns，机智如我
    }

    int type;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
            case KeyEvent.KEYCODE_HOME:
            case KeyEvent.KEYCODE_MENU:
            case KeyEvent.KEYCODE_VOLUME_DOWN:
            case KeyEvent.KEYCODE_VOLUME_UP:
            case KeyEvent.KEYCODE_VOLUME_MUTE:
        }
        return false;
    }

    /**
     * 设置popupwindow外面背景透明度
     *
     * @param bgalpha 透明度  0-1   0-透明   1-不透明
     */
    public void setBackgroundAlpha(float bgalpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgalpha;
        getWindow().setAttributes(lp);
    }



}